﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AppServicesHelloWorldApp.Services;
using Polenter.Services;

namespace AppServicesHelloWorldApp
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Private Field-Placeholder
        /// </summary>
        [Service]
        private ITimeProvider _timeProvider;


        public Form1()
        {
            InitializeComponent();

            // Inject Services
            AppServices.Instance.InjectServices(this);
        }

        private ITimer _timer;

        /// <summary>
        /// Public property Placeholder
        /// </summary>
        [Service]
        public ITimer Timer
        {
            get { return _timer; }
            set
            {
                if (_timer!=null)
                {
                    // unsubscribe the event
                    _timer.TenSecondsElapsed -= new EventHandler(on_timer_TenSecondsElapsed);
                }

                _timer = value;

                if (_timer != null)
                {
                    // subscribe the event
                    _timer.TenSecondsElapsed += new EventHandler(on_timer_TenSecondsElapsed);
                }
            }
        }

        void on_timer_TenSecondsElapsed(object sender, EventArgs e)
        {
            Text = _timeProvider.GetTime().ToString();
        }
    }
}
